from fake_useragent import UserAgent
import random


class RandomFakeHeaders(object):
    def __init__(self):
        self.__CSDN_REFERER = [
            "https://www.google.com.hk/",
            "https://cn.bing.com/",
            "https://www.baidu.com/",
            "https://blog.csdn.net/TOMOCAT",
        ]
        self.__XICI_REFERER = [
            "https://www.google.com.hk/",
            "https://blog.csdn.net/fitz1318/article/details/79463472",
        ]
        self.__VALID_REFERER = [
            "https://zhuanlan.zhihu.com/p/45093545",
            "https://cn.bing.com/",
        ]

    def random_headers_for_xici(self):
        headers = {
            "User-Agent": UserAgent().random,
            "Referer": random.choice(self.__XICI_REFERER),
            "Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Accept-Encoding": "gzip, deflate, br",
            "Cache-Control": "max-age=0",
            "Connection": "keep-alive",
            "Host": "www.xicidaili.com",
            "Upgrade-Insecure-Requests": "1"
        }
        return headers

    def random_headers_for_csdn(self):
        headers = {
            "User-Agent": UserAgent().random,  # 随机选择UA
            "Referer": random.choice(self.__CSDN_REFERER),
            "Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Accept-Encoding": "gzip, deflate, br",
            "Cache-Control": "max-age=0",
            "Connection": "keep-alive",
            "Host": "blog.csdn.net",
            "Upgrade-Insecure-Requests": "1"
        }
        return headers

    def random_headers_for_validation(self):
        headers = {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "close",
            "Host": "httpbin.org",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": UserAgent().random,
            "Referer": random.choice(self.__VALID_REFERER)
        }
        return headers


if __name__ == "__main__":
    print("Info: generate 20 random headers for xici:")
    for i in range(20):
        print(RandomFakeHeaders().random_headers_for_xici())
    print("Info: generate 20 random headers for csdn:")
    for i in range(20):
        print(RandomFakeHeaders().random_headers_for_csdn())
    print("Info: generate a random headers for validation:")
    print(RandomFakeHeaders().random_headers_for_validation())
